package jdl.tokenization.states;

import java.util.Hashtable;
import java.util.List;

import jdl.tokenization.Token;
import jdl.tokenization.TokenType;

public class NumberState implements IState {
	private List<Token> tokens;
	private Hashtable<Character, TokenType> hashtable;
	private IState state;
	private String content = "";

	public NumberState(List<Token> tokens,
			Hashtable<Character, TokenType> hashtable) {
		this.tokens = tokens;
		this.hashtable = hashtable;
		state = this;
	}

	@Override
	public void analyzeSymbol(char symbol) {
		if (!hashtable.containsKey(symbol)) {
			state = new WordState(tokens, hashtable);
		} else {
			if (hashtable.get(symbol) != TokenType.NUMBER) {
				if (!content.isEmpty()) {
					tokens.add(new Token(content, TokenType.NUMBER));
				}
				state = new InitialState(tokens, hashtable);
				state.analyzeSymbol(symbol);
			} else {
				content += symbol;
			}
		}
	}

	@Override
	public IState getNextState() {
		return state;
	}

	@Override
	public void endOfSymbols() {
		tokens.add(new Token(content, TokenType.NUMBER));
	}
}
